;; Tools generally useful for testing / debugging GHOST

(define-public (ghost-debug-mode)
 "
  Print the debug messages to stdout.
"
  (cog-logger-set-level! ghost-logger "debug")
  (cog-logger-set-stdout! ghost-logger #t))

; ----------
(define-public (ghost-debug-mode-off)
"
  Change the logger level to \"info\".
"
  (cog-logger-set-level! ghost-logger "info"))

; ----------
(define ghost-with-ecan #f)

(define-public (ecan-based-ghost-rules flag)
"
  For experimental purpose
  To create GHOST rules that are slimmer.
"
  (set! ghost-with-ecan flag)
)

; ----------
; TODO: Remove once experimentation is over
(define expt-var '())
; TODO: Should be removed as this is using 'ghost-find-rules',
; which will be obsolete soon
(define-public (test-ghost TXT)
"
  Try to find (and execute) the matching rules given an input TXT.
"
  (ghost TXT)
  (let ((rule (cog-outgoing-set (ghost-find-rules (ghost-get-curr-sent)))))
    (map (lambda (r) (psi-imply r)) rule)
    ; not using ghost-last-executed, because getting back to the rule
    ; from the alias atom is a hassle.
    (set! expt-var rule)
  )
  *unspecified*)

(define-public (ghost-action-executed?)
  (if (null? expt-var) (stv 0 1) (psi-action-executed? (car expt-var)))
)

; ----------
(define-public (ghost-get-result)
"
  Return the most recent result generated by executed an action.
"
  ghost-result)

; ----------
; For experimental purpose
; To get psi-rules only from the attentional focus (default),
; or get all psi-rules from the atomspace in case none of them reach
; the attentional focus
; Either way the rules will be selected based on their weights
(define ghost-af-only? #f)
(define-public (ghost-af-only AF-ONLY)
"
  To decide whether or not to get rules only from the attentional focus
  when doing action selection.
"
  (if AF-ONLY
    (set! ghost-af-only? #t)
    (set! ghost-af-only? #f)))

; ----------
(define-public (ghost-show-lemmas)
"
  Show the lemmas that havn been queried via 'get-lemma'.
"
  (display lemma-alist)
  (newline))

; ----------
(define-public (ghost-check-lemma WORD)
"
  Show the lemma of the word.
"
  (assoc-ref lemma-alist WORD)
)

; ----------
(define-public (ghost-add-lemma WORD LEMMA)
"
  Add an entry to the lemma list.
"
  (set! lemma-alist (assoc-set! lemma-alist WORD LEMMA))
)

; ----------
(define-public (ghost-remove-lemma WORD)
"
  Remove an entry from the lemma list.
"
  (set! lemma-alist (assoc-remove! lemma-alist WORD))
)

; ----------
(define-public (ghost-show-vars)
"
  Show the groundings of the user variables that has been assigned to
  some value.
"
  (format #t "=== User Variables\n~a\n" uvars))

; ----------
(define-public (ghost-get-curr-sent)
"
  Get the SentenceNode that is being processed currently.
"
  (define sent (cog-chase-link 'StateLink 'SentenceNode ghost-curr-proc))
  (if (null? sent) '() (car sent)))

; ----------
(define-public (ghost-get-curr-topic)
"
  Get the current topic.
"
  (gar (cog-execute! (Get (State ghost-curr-topic (Variable "$x"))))))

; ----------
(define-public (ghost-currently-processing)
"
  Get the sentence that is currently being processed.
"
  (let ((sent (ghost-get-curr-sent)))
    (if (null? sent)
        '()
        (car (filter (lambda (e) (equal? ghost-word-seq (gar e)))
                     (cog-get-pred (ghost-get-curr-sent) 'PredicateNode))))))

; ----------
(define*-public (ghost-get-relex-outputs #:optional (SENT (ghost-get-curr-sent)))
"
  Get the RelEx outputs generated for the current sentence.
"
  (parse-get-relex-outputs (car (sentence-get-parses SENT))))

; ----------
(define*-public (ghost-show-relation #:optional (SENT (ghost-get-curr-sent)))
"
  Get a subset of the RelEx outputs of a sentence that GHOST cares.
  SENT is a SentenceNode, if not given, it will be the current input.
"
  (define parses (sentence-get-parses SENT))
  (define relex-outputs (append-map parse-get-relex-outputs parses))
  (filter
    (lambda (r)
      (define type (cog-type r))
      (or (equal? 'ParseLink type)
          (equal? 'WordInstanceLink type)
          (equal? 'ReferenceLink type)
          (equal? 'LemmaLink type)))
    relex-outputs))
